home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2431 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.2 KB

  1. Path: news.voicenet.com!news
  2. From: kobak@voicenet.com (Peter Kobak)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overload of operator=
  5. Date: 17 Jan 1996 19:13:00 GMT
  6. Organization: Voicenet - Internet Access - (215)674-9290
  7. Message-ID: <4djhns$h6s@news.voicenet.com>
  8. NNTP-Posting-Host: philly227.voicenet.com
  9. X-Newsreader: NeoLogic News for OS/2 [version: 4.2]
  10.  
  11. In message <30FBCAA1.34A7@novell.com> - Greg Johnson <gregjo@novell.com> 
  12. writes:
  13. :>
  14. :>I want to overload the assignment operator, for some debugging purposes.
  15. :>But after I overload the operator, somehow, I need to perform the 
  16. :>default assignment. How do I do that if I don't know the exact size
  17. :>of the source or destination?
  18. :>How do I avoid recursion and perform the default behavior?
  19.  
  20. You assign the components of the object, not the binary representation.
  21.  
  22. :>For example:
  23. :>
  24. :>class BaseObj {
  25. :>public:
  26. :>    long x;       
  27. :>    BaseObj& operator=(BaseObj&);
  28. :>    BaseObj(void) : x(0) {};
  29. :>};
  30. :>
  31. :>BaseObj& BaseObj::operator=(BaseObj  & ptr)
  32. :>{
  33. :>    *this = ptr; // this will recursively call operator=
  34.     x = ptr.x;   // ** do this instead **
  35. :>    return *this;
  36. :>}
  37. :>
  38. :>void foo()
  39. :>{
  40. :>    BaseObj bo1;
  41. :>    BaseObj bo2;
  42. :>
  43. :>    bo1.x = 99;
  44. :>    bo2 = bo1;
  45. :>}
  46.  
  47. ================
  48. Peter Kobak
  49. kobak@voicenet.com
  50.  
  51.  
  52.